//
//  YsCloudFolderManager.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/2/26.
//

#import "YsCloudFolderManager.h"
#import "YsFileUtil.h"
#import "NSArray+QMUI.h"
#import "YsCloudFileManagerHeader.h"
#import "NSString+YscCloudFileTools.h"
#import "YsCloudFileRealmManager.h"
#import "YsCloudFileManageSDKRealm.h"
#import "YsCloudFileUploadReamModel.h"
#import "YsCloudFileRealmManager.h"
#import <YYKit/YYKit.h>

@implementation YsCloudFolderManager

+ (NSString *)getPathToCacheViedoWithAccountCode:(NSString *)accountCode withlastPathComponent:(NSString *)lastPathComponent
{
    NSString *fileName = [NSString stringWithFormat:@"%.0f%@",[NSDate date].timeIntervalSince1970,lastPathComponent];
    NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileUpload];
    NSString *srcPath = [folderPath stringByAppendingPathComponent:fileName];
    [YsFileUtil createDirectory:folderPath];
    return srcPath;
}

+ (NSString *)getPathToCacheViedoWithAccountCode:(NSString *)accountCode
                           withlastPathComponent:(NSString *)lastPathComponent
                                    withfileName:(NSString *)fileName
{
     NSString *name = [NSString stringWithFormat:@"%@.%@",fileName,lastPathComponent];
     NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileUpload];
    NSString *srcPath = [folderPath stringByAppendingPathComponent:name];
    [YsFileUtil createDirectory:folderPath];
    return srcPath;
}

+ (NSString *)getPathToCacheImageWithAccountCode:(NSString *)accountCode
                           withlastPathComponent:(NSString *)lastPathComponent
                                       withImage:(UIImage *)image
{
    SemaphoreBegin;
    NSData *data = UIImagePNGRepresentation(image);
    /// 获取文件名字
    NSString *name = [NSString stringWithFormat:@"%@%@",NSString.getUploadFileName,lastPathComponent];
    
    NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@/image",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileUpload];
    NSString *srcPath = [folderPath stringByAppendingPathComponent:name];
   [YsFileUtil createDirectory:folderPath];
    [data writeToFile:srcPath atomically:NO];
    SemaphoreEnd;
    return srcPath;
}

+ (NSString *)getPathToCacheDownloadFileWithAccountCode:(NSString *)accountCode withlastPathComponent:(NSString *)lastPathComponent withFileName:(NSString *)fileName
{
    NSString *name = [NSString stringWithFormat:@"%@%@",fileName,lastPathComponent];
      NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@%@",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileDownload];
      NSString *srcPath = [folderPath stringByAppendingPathComponent:name];
      [YsFileUtil createDirectory:folderPath];
      return srcPath;
}


+ (NSString *)getTmpDownloadPathTocacheFileWithAccountCode:(NSString *)accountCode withDownloadUrl:(NSString *)downloadUrl
{
    NSParameterAssert(accountCode);
    NSParameterAssert(downloadUrl);
    
    NSString *name = [NSString stringWithFormat:@"%@%@",downloadUrl.md5String,@".download"];
     NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@",YsFileUtil.getTmpPath,kCloudFileLocation,accountCode,kCloudFileDownload];
     NSString *srcPath = [folderPath stringByAppendingPathComponent:name];
    [YsFileUtil createFile:folderPath fileName:name];
    return srcPath;
}

+ (NSString *)getTmpDownloadPathTocacheFileWithAccountCode:(NSString *)accountCode
                                                  withName:(NSString *)name
{
    NSParameterAssert(accountCode);
    NSParameterAssert(name);
    NSString *fileName = [NSString stringWithFormat:@"%@%@",name.md5String,@".download"];
     NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@",YsFileUtil.getTmpPath,kCloudFileLocation,accountCode,kCloudFileDownload];
     NSString *srcPath = [folderPath stringByAppendingPathComponent:fileName];
    [YsFileUtil createFile:folderPath fileName:fileName];
    return srcPath;
}


+ (NSString *)getDownloadPathToCacheFileWithAccountCode:(NSString *)accountCode withlastPathComponent:(NSString *)lastPathComponent withName:(NSString *)name
{
    NSString *fileName = [NSString stringWithFormat:@"%@%@",name,lastPathComponent];
    NSString *folderPath = [NSString stringWithFormat:@"%@/%@/%@/%@",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileDownload];
    NSString *srcPath = [folderPath stringByAppendingPathComponent:fileName];
   [YsFileUtil createFile:folderPath fileName:fileName];
    return srcPath;
}

+ (BOOL)checkDownloadFileExistenceWithDownloadModel:(YsCloudFileDownloadModel *)downloadModel withAccountCode:(NSString *)accountCode
{
    NSString *fileName = [NSString stringWithFormat:@"%@%@",downloadModel.customCacheName,downloadModel.lastPathComponent];
     NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@/%@/%@",YsFileUtil.getLibraryCachePath,kCloudFileLocation,accountCode,kCloudFileDownload,fileName];
     BOOL isEist = [YsFileUtil fileExistsAtPath:filePath isDirectory:nil];
    return isEist;
}

+ (void)deleteLocalFileAndRealmDataWithuploadModel:(YsDocUploadModel *)uploadModel
{
    [YsFileUtil removeItemAtPath:uploadModel.filePath];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",uploadModel.accountCode];
      NSUInteger count = [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].count;
    if (count != 0) {
        YsCloudFileManageSDKRealm *cloudFileManageSDKRealm = (YsCloudFileManageSDKRealm *) [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].lastObject;
        int tmpIndex = -1;
        for (int i = 0; i < cloudFileManageSDKRealm.uploadReamModelArray.count; i++) {
            YsCloudFileUploadReamModel *uploadModel = [cloudFileManageSDKRealm.uploadReamModelArray objectAtIndex:i];
            if ([uploadModel.fileName isEqualToString:uploadModel.fileName]) {
                tmpIndex = i;
            }
        }
        if (tmpIndex != -1) {
            [YsCloudFileRealmManager updateObj:^{
                [cloudFileManageSDKRealm.uploadReamModelArray removeObjectAtIndex:tmpIndex];
            }];
            [YsCloudFileRealmManager deleteObj:uploadModel.fileUploadReamModel];
        }
    }
}

+ (void)deleteLocalFileAndRealmDataWithdownloadModel:(YsCloudFileDownloadModel *)downloadModel
{
        [YsFileUtil removeItemAtPath:downloadModel.cachePath];
       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",downloadModel.accountCode];
         NSUInteger count = [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].count;
       if (count != 0) {
           YsCloudFileManageSDKRealm *cloudFileManageSDKRealm = (YsCloudFileManageSDKRealm *) [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].lastObject;
           int tmpIndex = -1;
           for (int i = 0; i < cloudFileManageSDKRealm.downloadReamModelArray.count; i++) {
               YsCloudFileDownloadModel *downloadModel = [cloudFileManageSDKRealm.downloadReamModelArray objectAtIndex:i];
               if ([downloadModel.customCacheName isEqualToString:downloadModel.customCacheName]) {
                   tmpIndex = i;
               }
           }
           if (tmpIndex != -1) {
               [YsCloudFileRealmManager updateObj:^{
                   [cloudFileManageSDKRealm.downloadReamModelArray removeObjectAtIndex:tmpIndex];
               }];
               [YsCloudFileRealmManager deleteObj:downloadModel.downloadReamModel];
           }
       }
}

@end
